﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gpsys.Web.Models;
using Gpsys.Web.Common;
using System.Reflection;

namespace Gpsys.Web.Controllers
{
    public class PortController : BaseController
    {
        private void SetSelectedMenu()
        {
            ViewBag.SelectedMenu = "Port";
        }

        //
        // GET: /Port/

        public ActionResult Index()
        {
            SetSelectedMenu();
            PortModel model = new PortModel();
            return View(model);
        }

        public ActionResult Detail(int Id)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Detail Port";

            PortModel wfModel = new PortModel();
            var wf = wfModel.GetPort(Id);
            if (wf != null)
            {
                PortEntityModel m = new PortEntityModel()
                {
                    Port = wf
                };
                return View(m);
            }

            return RedirectToAction("e404", "Error");
        }

        [HttpPost]
        [ValidateInput(enableValidation: false)]
        public ActionResult Edit(PortEntityModel model)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Edit Port";
            ViewBag.Valid = true;
            ViewBag.IsUpdate = true;

            PortModel wfModel = new PortModel();

            wfModel.ValidatePort(ModelState, model);

            ViewBag.Valid = ModelState.IsValid;

            if (ModelState.IsValid)
            {

                model.Port.ModifiedBy = UserHelpers.UserName;

                if (wfModel.UpdatePort(model.Port))
                {
                    return RedirectToAction("Index");
                }
            }

            return View("Edit", model);
        }

        public ActionResult Edit(int Id)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Edit Port";

            ViewBag.IsUpdate = true;

            PortModel wfModel = new PortModel();
            var wf = wfModel.GetPort(Id);
            if (wf != null)
            {
                PortEntityModel m = new PortEntityModel()
                {
                    Port = wf
                };
                return View("Edit", m);
            }

            return RedirectToAction("e404", "Error");
        }

        public ActionResult Create()
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Create Port";
            ViewBag.IsUpdate = false;
            PortEntityModel model = new PortEntityModel()
            {
                Port = new Entity.MSG.Port()
                {
                    Type = 1
                }
            };
            return View("Edit", model);
        }

        [HttpPost]
        [ValidateInput(enableValidation: false)]
        public ActionResult Create(PortEntityModel model)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Create Port";
            ViewBag.Valid = true;
            ViewBag.IsUpdate = false;

            PortModel wfModel = new PortModel();

            wfModel.ValidatePort(ModelState, model);

            ViewBag.Valid = ModelState.IsValid;

            if (ModelState.IsValid)
            {
                model.Port.CreatedBy = UserHelpers.UserName;
                model.Port.ModifiedBy = UserHelpers.UserName;

                if (wfModel.InsertPort(model.Port))
                {
                    return RedirectToAction("Index");
                }
            }

            return View("Edit", model);
        }

        public ActionResult Delete(int Id)
        {
            SetSelectedMenu();

            PortModel wfModel = new PortModel();

            var wf = wfModel.GetPort(Id);
            if (wf != null)
            {
                wfModel.DeletePort(Id);

                return RedirectToAction("Index");
            }

            return RedirectToAction("e404", "Error");
        }

        public ActionResult GetAdapterXml(int type, string adapterType)
        {
            string data = "";
            PortEntityModel pem = new PortEntityModel();
            data = pem.GetAdapterXml(adapterType, type);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

    }
}
